# -*- coding: utf-8 -*-

from flask import Flask
import os

app = Flask(__name__)
profile = os.environ.get('DANTE_PROFILE', '')
#window开发环境变量就不设置了，转生产再改回来
app.config.from_object('config.' + "SMS" + 'Config')

import logging
from logging import Formatter
from logging.handlers import TimedRotatingFileHandler
from sms.database import db_session

#当出现异常时，放弃数据库读写操作
@app.teardown_request
def remove_db_session(exception):
    db_session.remove()

#初始化log记录模块
def init_logger():
    logger = logging.getLogger('SMS')
    handler = TimedRotatingFileHandler(app.config['LOG_FILE'], when="midnight")
    handler.suffix = '%Y-%m-%d'
    handler.setFormatter(Formatter('%(asctime)s %(module)s %(levelname)s: %(message)s ''[in %(pathname)s:%(lineno)d]'))
    app.logger.addHandler(handler)
    app.logger.setLevel(logging.INFO)
    logger.addHandler(handler)
    logger.setLevel(logging.INFO)

init_logger()

app.logger.info('SMS is using profile: [{0}]'.format(app.config['PROFILE']))

#注册路由应用模板
from sms.views import general
app.register_blueprint(general.mod)






